<template>
 <div class="home-box">
    <el-form :inline="true" :model="searchForm">
      <el-form-item label="考核条目">
        <el-input
          v-model="searchForm.name"
          placeholder="考核条目"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item label="创建时间" style="width: 18.75rem">
        <el-date-picker
          v-model="betweenTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="searchEvent" type="primary">查询 </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="update" type="primary">重置 </el-button>
      </el-form-item>
      <el-form-item >
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button circle icon="Refresh" @click="update" />
        </el-tooltip>
      </el-form-item>
    </el-form>
  </div>
  <el-table
    :data="KpiRuleList"
    border
    style="width: 95%; margin: 20px 0px; border-radius: 8px;box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);"
    :header-cell-style="{
      background: '#337ecc !important',
      color: '#ffffff',
      border: 'none !important',
      textAlign: 'center',
    }"
    :span-method="(value) => rowSpanRulesHandler(value, KpiRuleList)"
  >
    <el-table-column prop="name" label="KPI指标"></el-table-column>
    <el-table-column
      prop="target1,target2,rulePercent"
      label="规则"
      width="300"
      class-name="my-content"
    >
      <template #default="scope">
        {{ scope.row.target1 }}&gt;{{ scope.row.target2
        }}{{ scope.row.rulePercent }}%
      </template>
    </el-table-column>
    <el-table-column
      prop="target2,resultPercent"
      label="提成"
      width="150"
      class-name="my-content"
    >
      <template #default="scope">
        {{ scope.row.target2 }}
        <el-tag disable-transitions>{{ scope.row.resultPercent }}%</el-tag>
      </template>
    </el-table-column>
    <el-table-column
      prop="state"
      label="状态"
      width="82"
      align="center"
    >
      <template #default="scope">
        <el-tag
          :type="scope.row.state === 0 ?'danger' : 'success'"
          disable-transitions
          >{{ scope.row.state === 0 ? "未启用" : "启用" }}</el-tag
        >
      </template>
    </el-table-column>
    <el-table-column label="最后更新时间" width="200">
      <template #default="scope">
        <div style="display: flex; align-items: center">
          <el-icon><timer /></el-icon>
          <span style="margin-left: 10px">{{ scope.row.updateTime }}</span>
        </div>
      </template>
    </el-table-column>
  </el-table>
  <pagination
    class="pagination"
    v-show="KpiListPage.total > 0"
    :total="KpiListPage.total"
    v-model:page="searchForm.page"
    v-model:limit="KpiListPage.size"
    @pagination="handlePagination"
  />
</template>
<script setup>
import {
  ref,
  onMounted,
  onBeforeMount,
  reactive,
  defineEmits,
  watch,
} from "vue";
import { getKpiPastPage,getKpiPastSearch } from "@/api/kpi";
import { ElMessage } from "element-plus";

const betweenTime=ref(null)
const searchForm = ref({
  name:"",
  page: 1,
  page_size: 10,
  beginTime: "",
  endTime: "",
});
const KpiListPage = ref([]);
const KpiRuleList = ref([]);
onBeforeMount(() => {
  getKpiPastPage(searchForm.value).then((data) => {
    KpiListPage.value = data.data;
    KpiRuleList.value = data.data.records;
  });
});
watch(
  KpiRuleList,
  (newVal) => {
    
  },
  { deep: true }
);

const handlePagination=(value)=>{
  if(searchForm.value.name==""&&searchForm.value.betweenTime==""){
    getKpiPastPage(value).then(data=>{
        KpiListPage.value = data.data;
		KpiRuleList.value = data.data.records;
    })
  }
  else{
    searchForm.value.page=value.page;
    searchForm.value.page_size=value.page_size
    getKpiPastSearch(searchForm.value).then(data=>{
		KpiListPage.value = data.data;
		KpiRuleList.value = data.data.records;
    })
  } 
}

const searchEvent=()=>{
  if(betweenTime.value!=null){
    searchForm.value.beginTime=betweenTime.value[0]
    searchForm.value.endTime=betweenTime.value[1]
  }
  getKpiPastSearch(searchForm.value).then(data=>{
		KpiListPage.value = data.data;
		KpiRuleList.value = data.data.records;
    })
}

const update = (form) => {
  searchForm.value.name=""
  searchForm.value.beginTime=""
  searchForm.value.endTime=""
  searchForm.value.betweenTime=""
  getKpiPastPage(searchForm.value).then((data) => {
    if (data.code == 200) {
      ElMessage({
        message: "刷新成功",
        type: "success",
      });
      KpiListPage.value = data.data;
      KpiRuleList.value = data.data.records;
    } else {
      ElMessage.error("刷新失败");
    }
  });
};

const compareSingleAttrReturnColSpan = (row, compareAttr, index, tableData) => {
  let count = 0;
  let start = index;
  let multCompare = true;

  while (start < tableData.length && multCompare) {
    start++;
    count++;
    if (start <= tableData.length - 1)
      multCompare = row.id === tableData[start].id;
    else multCompare = row.id === tableData[start - 1].id;
  }
  start--; 

  if (start < tableData.length) {
    tableData[index][`${compareAttr}_count`] = count; 
  }

  if (index > 0 && count + 1 === tableData[index - 1][`${compareAttr}_count`]) {
    return [0, 0]; 
  }
  return [count, 1];
};

const columns = [
  "name",
  "target1",
  "target2",
  "state",
  "updateTime",
  "ins",
  "operate",
];
const rowSpanRulesHandler = ({ row, column, rowIndex, columnIndex }, data) => {
  if (
    columns[columnIndex] === "name" ||
    columns[columnIndex] === "ins" ||
    columns[columnIndex] === "operate"
  ) {
    const r = compareSingleAttrReturnColSpan(row, "emp_id", rowIndex, data);
    return r;
  }
};
</script>

<style lang="scss" scoped>
.title1 {
  color: rgb(64, 158, 255);
  width: 300px;
  height: 45px;
  font-size: 25px;
  background: rgba(64, 158, 255, 0.4);
  text-align: center;
  border-radius: 10px;
  margin: 10px auto 10px;
  line-height: 45px;
  border-top: #409eff 2px solid;
  border-bottom: #409eff 2px solid;
  box-shadow: 5px 5px 10px rgba(64, 158, 255, 0.5);
}
.pagination {
  margin: 20px 50px;
  display: flex;
  justify-content: end;
}
</style>